﻿using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Flock/Behavior/Seperation")]
public class SeperationBehavior : FlockBehavior
{
    public override Vector2 Force(FlockItem item, List<Transform> neighbors)
    {
        Vector2 force = Vector2.zero;
        Vector3 itemPos = item.transform.position;

        if (neighbors.Count == 0) return force;

        float radius = item.SeperateRadius;

        for (int i = 0; i < neighbors.Count; i++)
        {
            Transform neighborTF = neighbors[i];
            // 得到远离相邻单体的向量
            Vector2 offNeighbor = (Vector2) (itemPos - neighborTF.position);
            float length = offNeighbor.magnitude;
            // 让远离的向量大小随距离拉近增大
            offNeighbor /= length;

            // 如果距离过于接近，让远离的向量进一步放大，让单体更快远离
            if (length < radius)
            {
                offNeighbor *= item.SeperateForceMultiplier;
            }

            force += offNeighbor;
        }

        return force;
    }
}